<?php
class DB {
	function __construct($dbhost, $dbuser, $dbpw, $dbname) {
		if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
			$this->halt('cannot connrction database');
		}
		global $dbcharset;
		if($dbcharset) {
			@mysql_query("SET NAMES '$dbcharset'");
			mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
		}

		if($dbname) {
			mysql_select_db($dbname);
		}
	}
	//数据库
	function DB($dbhost, $dbuser, $dbpw, $dbname)
	{
		$this->__construct($dbhost, $dbuser, $dbpw, $dbname);
	}

	function fetch_array($query) {
		return mysql_fetch_assoc($query);
	}
	//数据查询
	function query($sql, $total = 0, $offset = 0)
	{
		if ($total > 0)
		{
			$sql .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
		}

		if(!($query = mysql_query($sql))) {
			$this->halt('MySQL Query Error:'.mysql_error(), $sql);
		}

		return $query;
	}
	//判断条件
	function in_set($field, $array, $negate = false)
	{
		$arraynum=count($array);
		if($arraynum == 0){
			if ($negate)
			{
				return '1=1';
			}
			else
			{
				return '1=0';
			}
		}
		if (!is_array($array)){
			$array = array($array);
		}
		if ($arraynum == 1){
			@reset($array);
			$var = current($array);
			return $field . ($negate ? ' <> ' : ' = ') . $this->validate_value($var);
		}else{
			return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, 'validate_value'), $array)) . ')';
		}
	}
	//验证数据
	function validate_value($var)
	{
		if (is_null($var))
		{
			return 'NULL';
		}
		else if (is_string($var))
		{
			return "'$var'";
		}
		else
		{
			return (is_bool($var)) ? intval($var) : $var;
		}
	}
	//插入数据
	function insert( $table, $array ,$key = false) {
		$sql = "INSERT INTO $table ( %s ) VALUES ( %s ) ";
		$fields = array();
		foreach ($array as $k => $v) {
			$fields[] = "$k";
			$values[] = "'$v'";
		}
		$sql = sprintf( $sql, implode( ",", $fields ) ,  implode( ",", $values ));
		$res=$this->query($sql);
		if($key){
			return mysql_insert_id();
		}else{
			return true;
		}

	}
	//更新数据
	function update( $table, $array, $key='') {
		if($key!=='')
		{
			$sql = "UPDATE $table SET %s WHERE %s";
		}else
		{
			$sql = "UPDATE $table SET %s";
				
		}
		$tmp = array();
		foreach ($array as $k => $v) {
			if( $k == $key ) {
				$where = $this->in_set($key, $v);
				continue;
			}
			if( $v == '' ) {
				$val = "''";
			} else {
				$val = "'$v'";
			}
			$tmp[] = "`$k`=$val";
		}
		$sql = sprintf($sql, implode( ",", $tmp ) , $where );
		$res=$this->query($sql);
		if($res==true){
			return true;
		}

	}
	//删除数据
	function delete($table, $keyname, $keys, $negate = false) {
		$res=$this->query("DELETE FROM $table WHERE ".$this->in_set($keyname, $keys, $negate));
		if($res==true){
			return true;
		}
	}
	//获取某行数据
	function getrow($sql)
	{
		$query = $this->query($sql);
		$rs =& mysql_fetch_assoc($query);

		return $rs;
	}
	//获得数据列表
	function getlist($sql, $total = 0, $offset = 0)
	{
		$query = $this->query($sql, $total, $offset);
		$results = array();
		while ($rs = mysql_fetch_assoc($query)) {
			$results[] = $rs;
		}
		return $results;
	}

	function error() {
		return mysql_error();
	}

	function errno() {
		return intval(mysql_errno());
	}
	
	//获得插入的id
	function insert_id() {
		$id = mysql_insert_id();
		return $id;
	}
	
	//转义
	function escaped( $text ) {
		return mysql_escape_string( $text );
	}
	
	//显示错误信息
	function halt($message = '', $sql = '') {
		//require_once (LOCAL_ROOT.'./include/db/db_mysql_error.inc.php');
		//echo mysql_error();
		echo $message;
		exit();
	}
	
	function getcount($table,$where='') {
		$num=mysql_num_rows($this->query('select * from '.$table.' '.$where));
		return $num;
	}
}
?>